
*====================================================
*====================Stata dofiles===================
*====================================================
*                                                                                                       
*----------------------Paper-------------------------
*                                                    
*-Dezhu Ye, Yew-Kwang NG, Yujun Lian*,               
* "Culture and Happiness",                           
* [Social Indicators Research], 2015, 123 (2): 519-547.      
* DOI: http://dx.doi.org/10.1007/s11205-014-0747-y.  
*                                                    
*----------------Corresponding Author---------------- 
*                                                     
*-Yujun Lian                                         
*                                                     
* Lingnan (University) College, Sun Yat-sen University
* Mail: arlionn@163.com                               
*                                                     
*-----------------------------------------------------


*==========
*-1- Setup
*==========

  *-Note:
  
  global path "D:\stata\personal\PX_B_2024b\B1_Test\paper\Lian_Culture_Happ" 
  adopath + "$path\adofiles"  // My ado files
  cd "$path"                  // data and results are saved here
  
  shellout "Ye_Ng_Lian_2014_Culture_and_happiness.pdf"  //our paper [PDF]
  
  
*=========
*-2- Data 
*=========
    
	clear all

  *-update the gdpper variable
    import excel using "Data\Culture_index.xlsx", clear firstrow sheet(Sheet1)
    rename _all, lower  // lower case
	renvars gdpper sci edu pop, suffix(_new)
	keep obs gdpper_new sci_new edu_new pop_new edu_middle
	save "Data\cul_gdpper_new.dta", replace
  
  *-add country id
    use "Data\Culture_happy_data.dta", clear 
  
    rename gdpper gdpall
	
    merge 1:1 obs using "Data\cul_gdpper_new.dta",  ///
      keepusing(gdpper_new sci_new edu_new pop_new edu_middle)
  
  *-merge with the laguage data
    merge 1:1 obs using "Data\language2.dta", nogen 
    drop year
	
  *-missing value
    local x "english chinese spanish french dutch catholic buddhist"
    foreach v of varlist `x'{
      replace `v'=0 if `v'==.
    }
  
  rename isc sci

  *-year
    split obs, parse(" - ")
    destring obs2, replace
    gen year = 1900+obs2 if obs2>10
    replace year = 2000+obs2 if obs2<10
	
  *-country id and name
    rename country country_str
    encode country_str, gen(country)
	gen id = real(obs1) 
	
  *-Varible definations
  
    gen gdp = gdpall/10^12  // 100-billion dollars

	*gen gdp_sci = (gdpper_new-sci_new)/gdpper_new
	
	rename sci sci0
	gen sci = ln(sci0)
	
	replace gdprate = gdprate/100    // percent/100
	replace pop = pop/100   // percent/100
	replace edu = edu/100   // percent/100	
	
    foreach v of varlist  hind hlong hmas hpdi huai{
	  replace `v' = `v'/10
	}
  
  *-variable lables
    labe var swb     "Subjective well-being"
	labe var gdp     "GDP in constant 2000 US 100-billion dollars"
	labe var gdprate "GDP growth rate, annual"
	labe var pop     "urban population density"
	labe var edu     "Enrollment ratio in tertiary education"
	labe var rights  "Human rights, scores in protecting individual’s civil or political rights"
	labe var sci     "log of Social comparison of income (SCI)"  
    char sci[one]    "The log of mean value of a nation’s bordering nations’ GDP per capita." 
	labe var ing 	 "In-group Collectivism (ING), House et al.(2004)"
	labe var inc 	 "Institutional Collectivism (INC), House et al.(2004)"
	labe var pdi 	 "Power Distance (PDI), House et al.(2004)" 
	labe var gei 	 "Gender Equality (GEI), House et al.(2004)" 
	labe var uai 	 "Uncertainty Avoidance (UAI), House et al.(2004)" 
	labe var aoi 	 "Assertiveness (AOI), House et al.(2004)" 
	labe var foi 	 "Future Orientation (FOI), House et al.(2004)" 
	labe var hoi 	 "Humane Orientation (HOI), House et al.(2004)" 
	labe var poi 	 "Performance Orientation (POI), House et al.(2004)" 
	labe var hlong   "The long-term orientation (H-Long), Hofstede_Bond_1988"
	labe var huai    "uncertainty avoidance index (H-UAI), Hofstede_Bond_1988"
	labe var hind    "individualism index (H-IND), Hofstede_Bond_1988"
	labe var hpdi    "power distance (H-PDI), Hofstede_Bond_1988"
	labe var hmas    "masculinity(H-MAS), Hofstede_Bond_1988"	
	labe var english "1=English is official language; 0=otherwise"
    labe var dutch   "1=Dutch is official language; 0=otherwise"
  
  *-定义存储变量的全局暂元
    global y  "swb"                                 // Dependent Variable
    global xx "gdp gdprate edu rights pop sci"      // Control Variables
    global cc "ing inc pdi gei uai aoi foi hoi poi" //GLOBE cultural indices, 
                                                    //See: House et al. (2004)
    global ch "hind hlong hmas hpdi huai"  //Hofstede cultural indices, 
                                           // See: Hofstede and Bond (1988)
    global lg "english chinese spanish french dutch" //Language Dummy, 
                                          // See: Stulz and Williamson (2003)
    global all "$y $xx $cc $ch $lg"  
  
  
  *-标记/删除缺漏值 missing values

    *-标记缺漏值
      reg swb $xx $cc 
      gen oursample = e(sample)  
		  
    *-删除缺漏值
      keep if oursample==1  //442个观察值
 
    *-样本分布
      xtset id year 
      xtdes, p(30)   //样本分布
      panels id: tab year
  
  *-Variables order 
    order country id year swb happy           ///
	      gdp* edu right pop sci              ///
		  ing inc pdi gei uai aoi foi hoi poi ///
	      hind hlong hmas hpdi huai           ///
		  english-dutch
   
   
   
*===============
*-3- Statistics 
*===============

  
*---------
*-Table 2. Definitions and sources of variables
*---------  

  logout, save("Results\Table02") excel replace: ///
          des $all
  
  des2 $all
 
/*
variable |   variable label
---------|---------------------------------------------------------
swb      |   Subjective well-being
gdp      |   GDP in constant 2000 US 100-billion dollars
gdprate  |   GDP growth rate, annual
edu      |   Enrollment ratio in tertiary education
rights   |   Human rights, scores in protecting individual’s civil or political rights
pop      |   urban population density
sci      |   log of Social comparison of income (SCI)
ing      |   In-group Collectivism (ING), House et al.(2004)
inc      |   Institutional Collectivism (INC), House et al.(2004)
pdi      |   Power Distance (PDI), House et al.(2004)
gei      |   Gender Equality (GEI), House et al.(2004)
uai      |   Uncertainty Avoidance (UAI), House et al.(2004)
aoi      |   Assertiveness (AOI), House et al.(2004)
foi      |   Future Orientation (FOI), House et al.(2004)
hoi      |   Humane Orientation (HOI), House et al.(2004)
poi      |   Performance Orientation (POI), House et al.(2004)
hind     |   individualism index (H-IND), Hofstede_Bond_1988
hlong    |   The long-term orientation (H-Long), Hofstede_Bond_1988
hmas     |   masculinity(H-MAS), Hofstede_Bond_1988
hpdi     |   power distance (H-PDI), Hofstede_Bond_1988
huai     |   uncertainty avoidance index (H-UAI), Hofstede_Bond_1988
english  |   1=English is official language; 0=otherwise
chinese  |   
spanish  |   
french   |   
dutch    |   1=Dutch is official language; 0=otherwise
---------|---------------------------------------------------------
*/

*---------
*-Table 3. Description of Variables  
*---------
   
*-Minor difference with results reported in the paper
  logout, save("Results\Table03") excel replace: ///
    tabstat $all, s(N mean p50 sd min max) column(stat) f(%4.2f)		  
	
		  
*---------
*-Table 4. Correlation matrix
*---------

  logout, save("Results\Table04") excel replace: ///  
    pwcorr_c $y $xx $cc, star(0.05) format(%7.2f)  // Table 4 in the paper


  *-VIF (not reported in the paper)

    reg swb $xx
    estat vif
	
    reg swb $xx $cc
    estat vif  

	
	
*===============
*-5- Regression 
*===============

*---------
*-Table 5. OLS regression
*---------

  global vce "robust"  //仅考虑异方差
 *global vce "vce(cluster id)" //同时考虑异方差和组内相关性
  
*-Column (1)
  reg swb $xx, $vce
  est store OLS
*-Column (2)  
  dropvars dumid*
  tab country, gen(dumid)
  drop dumid1-dumid4
  reg swb $xx dumid*, $vce
  est store CFE
  xtdes if e(sample), p(30)  //实际参与回归的样本数
*-Column (3)  
  reg swb $xx ing, $vce
  est store ing
  xtdes if e(sample)
*-Column (4)  
  reg swb $xx inc, $vce
  est store inc
*-Column (5)   
  reg swb $xx pdi, $vce
  est store pdi
*-Column (6)  
  reg swb $xx gei, $vce
  est store gei  
*-Column (7)  
  reg swb $xx uai, $vce
  est store uai   
*-Column (8)  
  reg swb $xx aoi, $vce
  est store aoi  
*-Column (9)  
  reg swb $xx foi, $vce
  est store foi  
*-Column (10)  
  reg swb $xx hoi, $vce
  est store hoi 
*-Column (11)  
  reg swb $xx poi, $vce
  est store poi
*-Column (12)  
  reg swb $xx $cc, $vce
  est store all
*-Column (13)  
  global lg "english"        //language dummy 
  reg swb $xx $cc $lg, $vce  //control language
  est store all_lg  

 *-Tabulate the results in [-Table 5-]
   local save "using Results\Table05.csv"  //执行时选中此行可以把结果输出到 Excel 中
   local mm "OLS CFE ing inc pdi gei uai aoi foi hoi poi all all_lg"  
   esttab `mm' `save', mtitle(`mm') nogaps replace compress ///
          b(%6.3f) star(* 0.1 ** 0.05 *** 0.01)             ///
          scalar(N r2 r2_a) keep($xx $cc _cons) 

		
		
*=================================
*-6- Relative Importance Analysis 
*=================================
	  
*---------
*-Table 6. Relative importance (RI) analysis 
*--------- 
 
*------------------------- 
*-Table 6: Column (3)-(11)

 *--------------------------------------------Begin------
  dropvars RI* Rank*
  mat drop _all
  local j=1
  *-Note: global cc "ing inc pdi gei uai aoi foi hoi poi"
  foreach v of varlist $cc{ 
    domin swb $xx `v' , reg(regress) 
	dominA a_`v', rank dta(a_`v') percent 
	mat colname a_`v' = `v' rank
	mat RIA = (nullmat(RIA), a_`v')
	local j = `j'+2
  }
  format RI* %3.1f
  br  RIa_ing1-Ranka_poi1   //浏览数据
  
  *-Export Column (3)-(11) to Excel
    logout, save("Results\Table06_A") excel replace: ///
       mat list RIA, format(%4.1f)	   
 *---------------------------------------------Over------
 *-Note: -dominA.ado- is given by -Lian Yujun-, 
 *       list in the end of this dofile	

*~~~~~~~~~~~~~~~~~~~~~~~~~部分结果展示~~~~~~~~~~~~~~~~~~~~~~~~begin~~~~ 
*
*-Example: ing  (全局暂元 cc 中的第一个变量) 
Total of 127 regressions

Computing all predictor combinations

Progress in running all regression subsets
0%------50%------100%
....................

General dominance weights
Number of obs             =  442
Overall Fit Statistic     =  0.3630

            |      Dominance      Standardized      Ranking
 swb        |      Weight         Weight
------------+-----------------------------------------------
 gdp        |      0.0065         0.0179            6 
 gdprate    |      0.0050         0.0138            7 
 edu        |      0.0228         0.0629            4 
 rights     |      0.0558         0.1536            3 
 pop        |      0.0149         0.0411            5 
 sci        |      0.1430         0.3940            1 
 ing        |      0.1149         0.3165            2 
------------------------------------------------------------
*~~~~~~~~~~~~~~~~~~~~~~~~~部分结果展示~~~~~~~~~~~~~~~~~~~~~~~~~over~~~~ 
 
*--------------------- 
*-Table 6: Column (12)
 
  dropvars RI* Rank*
  global x1 "gdp gdprate edu right pop"
  global x2 "sci"
  domin swb $cc , reg(regress) sets(($x1) ($x2)) 
 *domin swb sci $cc , reg(regress) sets(($x1))   //same as above
  dominA a_all, rank dta(a_all) percent 
  format RI* %3.1f
  br RIa_all1 Ranka_all1
	
  *-Export Column (12) to Excel, 
  * minor difference with results reported in the paper
    logout, save("Results\Table06_B") excel replace: ///  
	   mat list a_all

	   
	   
*=======================
*-7- Endogeneity Issues 
*=======================
	
*---------
*-Table 7. National culture and SWB (GMM estimation)
*--------- 	
	
  *---------------
  *-GMM estimation

    global First ""  // to be used to save estimation results of first stage
    global IVs "chinese english spanish french dutch catholic buddhist" 
	global cc "ing inc pdi gei uai aoi foi hoi poi"
	foreach v of varlist $cc{
	  dis _n(4) _dup(40) in red "-" 
	  dis in red "variable = " in y "`v'"
	  dis _dup(40) in red "-"
	  ivregress gmm swb $xx (`v'=$IVs), small // GMM
	  est store `v'
	  *-First stage results
	  qui reg `v' $IVs
	  est store f_`v'
	  global First "$First f_`v'" // stack results
	}
	
  *---------------------
  *-Tabulate the results
  
   local save "using Results\Table07.csv" 
   local mm "ing inc pdi gei uai aoi foi hoi poi"  
   esttab `mm' `save', mtitle(`mm')  b(%6.3f) nogaps replace  ///
          star(* 0.1 ** 0.05 *** 0.01) order($xx $cc) compress  ///
          scalar(N r2 r2_a C p_C HansenJ p_Hansen)  
    
  *--------------------
  *-First stage results (Not reported in the paper)
  
   local save "using Results\Tab07_GMM_first.csv"  
   local mm "$First"  
   esttab `mm' `save', mtitle(`mm')  b(%6.3f) nogaps replace  ///
          star(* 0.1 ** 0.05 *** 0.01) keep($IVs _cons) compress  ///
          scalar(N r2 r2_a)
	
	
		  
*====================
*-8- Robustness Test 
*====================

*---------
*-Table 8. National culture and SWB (Hofstede culture index)
*--------- 	

*-OLS  
  global mm ""
  global ch "hind hlong hmas hpdi huai"
  foreach v of varlist $ch{
     reg swb $xx `v', $vce
     est store `v'
	 global mm "$mm `v'"
  }
  
*-GMM
  global m1 ""  //first stage results
  foreach v of varlist $ch{
     ivregress gmm swb $xx (`v'=$IVs), small
     est store `v'_gmm   
	 global mm "$mm `v'_gmm"
	 qui reg `v' $IVs
	 est store f_`v'
	 global m1 "$m1 f_`v'"
  }
  
*--Tabulate the results (Table 8)
   local save "using Results\Table08.csv"  
   esttab $mm `save', mtitle($mm)  b(%6.3f) nogaps replace   ///
          star(* 0.1 ** 0.05 *** 0.01) keep(`keep') compress ///
          scalar(N r2 r2_a F)    
  
    
*-First stage results (Not reported in the paper)
   local save "using Results\Tab08_robust_first.csv"  
   *local mm "hind hlong hmas hpdi huai "  
   esttab $m1 `save', mtitle($mm)  b(%6.3f) nogaps replace  ///
          star(* 0.1 ** 0.05 *** 0.01) keep($IVs _cons) compress  ///
          scalar(N r2 r2_a F)    
  

*-------------------------------OVER--------------------------------
  
  
  
*-----------------------------------
*------------my adofiles------------
*-----------------------------------
/*
  cap program drop dominA
  program define dominA
  version 12.0
    syntax namelist(max=1) [, Rank Brackets Dta(string) Nodisplay Percent]
	mat std_a = e(std)'
	if "`percent'"==""{
	   mat RI = std_a
	}
	else{
	   mat RI = std_a*100
	}
	if "`rank'"==""{
	  mat `namelist' = RI
	  mat colnames `namelist' = RI(%)	  
	}
	else{
	  mat rank  = e(ranking)'
	  mat `namelist' = (RI, rank)
	  mat colnames `namelist' = RI(%) rank
	}
	if "`nodisplay'" == ""{
	  mat list `namelist'
	}
	if "`dta'" != ""{
	  if "`rank'"!=""{
	    svmat RI, names(RI`dta')
	    svmat rank, names(Rank`dta')
		if "`brackets'"!=""{
		  tostring Rank`dta', replace
          replace Rank`dta' = "[" + Rank`dta' + "]"
		}
	  }
	  else{
	    svmat RI, names(RI`dta')
	  }
	}
  end  
 *---------------------------------------
*/  
  
  
